Grokking Scalable Systems for Interviews
Rate this course
Ask Author
Contribute
Back to course home

4% completed

Vote For New Content
Introduction
Course Overview
Who This Course Is for
Caching
What Is Negative Caching and When Should You Cache 404 or Empty Results?
What Is the Difference Between Soft TTL and Hard TTL, and Why Does It Matter?
How Can a Bloom Filter Reduce Cache or Database Load?
What Is Distributed Locking for Cache Rebuilds, and How Does It Prevent Cache Stampedes?
Load Balancing
What Are Sticky Sessions (Session Affinity) and When Should I Avoid Them?
What Is the Difference between Liveness Checks and Readiness Checks in Load Balancers?
What is Global Server Load Balancing (GSLB) and How GeoDNS and Anycast Work?
API Basics
What are the Main API Pagination Strategies (offset, cursor, keyset), and When Should I Use Each?
What are HTTP Conditional Requests (ETag, If‑None‑Match, Last‑Modified) and How They Reduce Load?
What Is the Difference between Rate Limiting and Throttling and Quotas?
What Are Idempotency Keys and How to Implement Them Safely for Payments
Databases
What are SQL Isolation Levels (Read Committed, Repeatable Read, Serializable), and What Anomalies Do They Prevent?
What Is a Write‑Ahead Log (WAL), and How Does It Ensure Durability?
What Is the Difference Between Synchronous and Asynchronous Replication, and When Should I Use Read Replicas?
What Is MVCC (Multi‑Version Concurrency Control), and How Does It Enable Concurrent Reads and Writes?
What Are Safe Patterns for Online Schema Changes vs. Offline Migrations?
Scalability
What is Little’s Law and How to Use It for Quick Capacity Estimates in System Design
How Can I do Back-of-the-Envelope Sizing for QPS, Bandwidth, Storage, and Peak vs. Average Load?
What are Cold Starts and Warm Starts, and Why Do They Matter for Performance?
Sharding and Partitioning
How Do I Choose a Good Shard Key and Avoid Hotspots (Bucketing and Key Randomization)?
What Are Range Sharding, Directory‑Based Sharding, and Geo‑Sharding, and What Are the Common Use Cases?
What Is the Difference Between Rendezvous Hashing and Consistent Hashing, and When Should I Use Each?
Messaging and Streaming
What Do At‑most‑once, At‑least‑once, And Exactly‑once Delivery Semantics Mean?
What Are Idempotent Producers And Consumers, And How Do De‑duplication Keys Work?
What Is Message Ordering, How Do Partition Keys Affect It, And When Can Ordering Break?
What Are Windowing And Watermarking In Streaming Systems, In Simple Terms?
What Is Quorum (N, R, W), And Why Does R + W > N Give Strongly Consistent Reads?
What Are Common Conflict Resolution Strategies (Last‑write‑wins, Vector Clocks, Logical Clocks)?
What Are Read Repair, Hinted Handoff, And Anti‑entropy (Merkle Trees) In Eventually Consistent Systems?
What Is The Difference Between SLI, SLO, And SLA, And Can You Give Simple Examples?
What Is Opentelemetry, And How Do Traces, Spans, Metrics, And Logs Fit Together?
What Is An Error Budget, And How Should It Guide Release Decisions?
What Are The Trade‑offs Between Server‑stored Sessions And JWTs for Authentication?
What Is The Difference Between TLS And MTLS, And When Is MTLS Appropriate For Service‑to‑service Auth?
What Is Secrets Management, And How Do Environment Variables, KMS, And Vault Compare?
What Is A Replay Attack, And How Is It Different from Idempotency Issues?
What Are The Differences Between TCP, UDP, And QUIC, And When Should I Use Each?
What Is The Difference Between A Forward Proxy, A Reverse Proxy, And NAT?
What Changed From HTTP/1.1 To HTTP/2 To HTTP/3, And What Are Head‑of‑line Blocking And Multiplexing?
What Is the Difference Between Content‑Addressable Storage and Location‑Based Addressing?
What Are Checksums and Etags, and How Do MD5/SHA Compare to CRC for Integrity?
What Are Hot, Warm, Cold, and Archive Storage Tiers, and When Should I Use Each?
What Is the Outbox Pattern, and How Does Change Data Capture (CDC) Work at a High Level?
What Is CQRS (Command Query Responsibility Segregation), and When Should I Consider It?
What Are the Differences Between a Circuit Breaker, Retry With Backoff, and Rate Limiting?
What Are RPO and RTO, and How Do They Differ in Disaster Recovery Planning?
What Is the Difference Between Active‑Active and Active‑Passive Architectures?
What Is Graceful Degradation, and How Do Feature Flags Help Availability?
What Are the Differences Between UUID, ULID, KSUID, and Snowflake IDs, and How Do I Choose?
How Do Snowflake‑Style IDs Work (Timestamp, Worker, Sequence), and What Problems Do They Solve?
What Is the Difference Between Wall‑Clock Time and Monotonic Time, and Why Is Clock Skew Hard?
Final Words
Key Takeaways
What To Learn Next
What Is Message Ordering, How Do Partition Keys Affect It, And When Can Ordering Break?

Message ordering is the guarantee that a messaging system delivers messages to consumers in the same sequence that the producer sent them.

In simpler terms, it means if messages are sent as 1, 2, 3, they should be received and processed as 1, 2, 3 in that exact order.

This ordered delivery is crucial for preserving the chronology of events in many applications.

Most messaging and streaming systems try to maintain FIFO (first-in, first-out) behavior under normal conditions, but you must design carefully for scenarios that could disrupt this order.

Why Message Ordering Matters

Message ordering ensures data consistency and correct application logic.

If messages arrive out of order, it can lead to incorrect outcomes or confusion.

For example, imagine a banking system where a deposit event and a withdrawal event for the same account are processed out of sequence. A withdrawal processed before its corresponding deposit could make an account balance go negative erroneously.

Similarly, an e-commerce workflow might break if a “item shipped” event is handled before the “item packed” event due to misordered messages.

These scenarios show that when the message sequence is wrong, business logic can fail, resulting in data corruption or invalid states.

In applications like seat reservations, order is vital: a message to reserve a seat must be processed before a subsequent message to cancel that reservation.

In general, any system that models real-world processes (orders, transactions, status updates) relies on the chronological ordering of events.

Preserving message order means the consumer sees events in the intended sequence, making the system behavior predictable and easier to reason about.

Messaging systems (queues, streams, pub/sub services, etc.) often guarantee message ordering by default in simple setups.

For instance, a single queue with one consumer will typically deliver messages in the same order they were enqueued.

However, as systems scale out (with parallel consumers, multiple partitions, or distributed brokers), maintaining a single global order becomes challenging.

It’s important to understand how modern streaming platforms handle ordering so we can avoid pitfalls that break the sequence of messages.

Message Ordering
Message Ordering

Partition Keys and Message Ordering

Many modern streaming and messaging platforms (like Apache Kafka, Amazon Kinesis, Google Pub/Sub, etc.) use partitions to achieve high throughput.

A partition is essentially a sub-stream or sub-queue that allows messages to be processed in parallel across different servers.

Partitioning improves scalability, but it introduces a trade-off: ordering is guaranteed within each partition, but not across different partitions.

This is where partition keys (also known as message keys or ordering keys) come into play.

A partition key is an attribute (often a string or ID) attached to messages to influence how they are routed to partitions.

In systems like Kafka, the default behavior is to hash the key to determine the partition for a message.

All messages with the same key will consistently map to the same partition, which means they will be in one ordered sequence on that partition.

Effectively, the partition key defines a grouping for messages such that each group (key) has its own ordered timeline.

Using partition keys is the primary way to preserve message ordering for related events.

By choosing an appropriate key, you ensure related messages aren’t split across partitions.

For example, if you use a customer’s ID as the partition key in an e-commerce application, then all events related to that customer (order placed, payment made, item shipped) will go to the same partition and thus remain in the correct chronological order.

Similarly, in a banking system, using an account ID as the key means deposits and withdrawals for that account won’t get interleaved with others; they’ll stay in sequence on one partition.

This design preserves consistency. Each user or entity sees events happen in a logical order.

On the other hand, if no key is specified (or if a system uses a round-robin or random distribution), messages will be spread across partitions without regard to relatedness.

This can break ordering for related events.

A round-robin strategy, for instance, evenly load-balances messages but allows even messages with the same logical relationship to end up on different partitions.

That means if two events should be ordered but have no key tying them together, they might be processed by different partitions (or consumers) concurrently, and one could overtake the other.

In Kafka, messages without a key are often distributed in a way that optimizes throughput (e.g. sticky round-robin batching) rather than order, so you only get ordering guarantees accidentally when using a single partition or slow production of messages.

Partition keys directly affect ordering guarantees: Kafka’s rule of thumb is that ordering is per partition, so picking the right key ensures all messages that need ordering end up in the same partition sequence.

Google Cloud Pub/Sub offers a similar concept called an ordering key, which serves the same purpose of defining an order group for messages (with the caveat that all messages with the same ordering key should be in the same region for Pub/Sub).

In any partitioned or sharded messaging system, the concept is the same. You define a key for which messages must be kept in order, and the system routes those messages to a single ordered stream.

Be aware of key choice and cardinality: The choice of partition key can affect not only ordering but also load distribution.

A key that is too broad (e.g. a constant or only a few values) will send many messages to one partition (potentially a “hot” partition), while a key that is too unique (like a UUID per message) might negate grouping benefits.

The key should be chosen based on which messages truly need ordering relative to each other (for example, by customer, by order, by account, etc.), and there should be enough different key values to spread traffic reasonably.

This ensures you get both ordering where it matters and parallelism where possible.

Finally, it’s important to note that ordering guarantees hold as long as the partitioning scheme remains consistent.

If the number of partitions changes or the partition key strategy is altered, the existing ordering guarantees can be upset.

For instance, if you increase the number of partitions for a topic after having already produced messages, the mapping of keys to partitions may change.

A message with key “X” that used to go to Partition 1 might now go to a new Partition 5 after rehashing, meaning new messages could be on a different partition than older messages with the same key.

In Kafka, adding partitions to an existing topic requires careful thought because it can disrupt message ordering for keys that get remapped.

In short, partition keys give you scope-limited ordering (within that key’s partition), and you must design your system such that any data that needs total order uses the same key (and partition), and that partitioning remains stable over time.

When Can Message Ordering Break?

Even with careful design, there are scenarios where message ordering can break down, meaning you might observe messages arriving or being processed out of their intended order.

Below are common situations that can lead to out-of-order messages:

  • Multiple Partitions (No Global Order): If messages are spread across multiple partitions, there is no single global ordering guarantee across those partitions. Each partition delivers its messages in order, but a consumer reading from multiple partitions may see interleaved messages. One partition’s message might overtake another’s because it was processed faster or had less backlog. In essence, if two related events land on different partitions, the consumer could receive them out of sequence simply because one partition is slower or faster than the other. For example, Partition A’s message “Order Placed” might arrive after Partition B’s message “Order Shipped” due to parallel processing, confusing the overall timeline.

  • Concurrent Consumers and Parallel Processing: In queue systems or consumer groups, having multiple consumers in parallel can break ordering unless each message group is siloed to one consumer. If two messages intended to be in order are consumed by different workers at the same time, the one that finishes processing first will effectively be handled out of order. For instance, in a classic queue with two consumer threads, one thread might grab message 1 and another grabs message 2; if the second thread finishes quickly, message 2 gets processed before message 1. In general, when multiple consumers share the load, a later message can be handled before an earlier one is fully processed. This is why Kafka restricts one partition to only one consumer within a group to avoid concurrent consumption of the same partition (thus preserving order in that partition). But across partitions or in systems without that restriction, parallelism can scramble the order of processing.

  • Missing or Inconsistent Partition Keys: Ordering can break if related messages are not sent with a consistent key. If one event in a sequence is published with a certain key but the next event mistakenly uses a different key (or no key), those events will be routed to different partitions, losing their relative order. For example, if a user’s session events are keyed by SessionID, but one event accidentally uses a different ID or null key, it may go to another partition and arrive out-of-sync. Always use the same partition key for events that must be ordered together; otherwise, the system will treat them as unrelated and you may see unexpected reordering.

  • Changing Partition Counts or Repartitioning: As mentioned, increasing the number of partitions for a topic (repartitioning) after data is already flowing can disrupt the established order. When partition counts change, the hashing function’s range changes, and keys can get mapped to new partitions, breaking the continuity of ordering for those keys. Essentially, message ordering that was guaranteed within a partition is no longer guaranteed if new messages with the same key start going to a different partition than earlier messages. This is a one-time reconfiguration issue, but it can be significant if not planned for. The safe approach is to set an appropriate number of partitions from the start, or if you must increase partitions, understand that past ordering guarantees might reset beyond that point.

  • Network Delays and Retries: In distributed systems, network issues or crashes can also cause apparent reordering. If a message gets delayed in transit or a broker temporarily goes down, a later message might overtake it in delivery. Moreover, with at-least-once delivery semantics, if a message fails processing and is retried or redelivered, it may arrive after messages that were originally behind it. For example, message #2 might not be acknowledged, causing the broker to resend it, but by the time it arrives again, the consumer has already received #3 and #4. This results in an observed sequence like 1, 3, 4, then 2. Retries and redeliveries can shuffle the order in which messages are finally processed. While the underlying log (in Kafka) still has the original order by offset, the act of redelivery or failure handling can make the application see them out of order unless special care is taken.

  • Message Priorities or Selective Consumers: Some messaging systems allow assigning priority levels to messages or allow consumers to filter/select specific messages. These features can inherently violate FIFO ordering. A higher-priority message might jump ahead of lower priority ones in the queue. Likewise, a consumer that requests a specific subset of messages (using selectors or filters) might pick a message that was behind others, effectively skipping the line. In JMS, for instance, a message with a higher priority can overtake lower priority messages, and using selectors can result in consuming messages out of the natural order. If an application mixes different priority levels or selective consumption on the same channel, the delivered order will no longer strictly match the send order.

Each of these scenarios illustrates how ordering guarantees can break if we’re not careful.

The key takeaway is that ordering is not absolute in a distributed system. It’s usually conditional, applying within certain scopes (a single queue, a single partition, a single consumer thread, etc.).

When we scale out or introduce complexity, we have to manage that scope of ordering.

Many robust systems combine strategies (like using partition keys for grouping, plus adding sequence numbers or timestamps in the message data for verification) to handle out-of-order events gracefully.

What Are Idempotent Producers And Consumers, And How Do De‑duplication Keys Work?
What Are Windowing And Watermarking In Streaming Systems, In Simple Terms?
Mark as Completed

On this page

Why Message Ordering Matters

Partition Keys and Message Ordering

When Can Message Ordering Break?